package com.qifn.blog.Controller;

import com.github.pagehelper.PageInfo;
import com.qifn.blog.Dao.BlogDao;
import com.qifn.blog.Model.Blog;
import com.qifn.blog.Model.Tag;
import com.qifn.blog.Model.Type;
import com.qifn.blog.Service.BlogService;
import com.qifn.blog.Service.TagService;
import com.qifn.blog.Service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/")
public class IndexController {

    @Resource
    BlogService blogService;

    @Resource
    TypeService typeService;

    @Resource
    TagService tagService;

    @GetMapping
    public String toIndex(){
        return "redirect:/index";
    }

    @GetMapping("/index")
    public ModelAndView index(@RequestParam(defaultValue = "1") Integer pageNum){
        ModelAndView mv = new ModelAndView();
        PageInfo<Blog> blogPageInfo = blogService.indexQueryBlog(pageNum,4);
        List<Type> typeList = typeService.queryAdminIndexAllType();
        List<Tag> tagList = tagService.queryAdminReleaseAllTag();
        List<Blog> recommendBlog = blogService.indexRecommendBlog();
        mv.addObject("recommendBlog",recommendBlog);
        mv.addObject("typeList",typeList);
        mv.addObject("tagList",tagList);
        mv.addObject("blogPageInfo",blogPageInfo);
        mv.setViewName("index");
        return mv;
    }

    @GetMapping("/blog")
    public ModelAndView blog(Integer blogId){
        ModelAndView mv = new ModelAndView();
        Blog blog = blogService.queryBlogByBlogId(blogId);
        mv.addObject("blog",blog);
        mv.setViewName("blog");
        return mv;
    }

    @GetMapping("/search")
    public ModelAndView search(@RequestParam(defaultValue = "1") Integer pageNum,String keyword){
        ModelAndView mv = new ModelAndView();
        PageInfo<Blog> blogPageInfo = blogService.indexSearchBlog(pageNum,4,keyword);
        mv.addObject("keyword",keyword);
        mv.addObject("blogPageInfo",blogPageInfo);
        mv.setViewName("blogsearch");
        return mv;
    }
}
